package com.slz3.business.module.memberManagement.Feedback.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conFeedback.domain.ConFeedback;
import com.slz3.business.module.configManagement.conFeedback.domain.FeedbackVo;
import com.slz3.business.module.memberManagement.Feedback.domain.Feedback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @FileName: MemMemberSynr
 * @Description:会员意见反馈同步
 */

@Component
@Slf4j
public class MemFeedBackSync {

    /**
     * 会员意见反馈同步处理方法
     */
    public static void syncData(){

        try {
            Object obj = Jedis.db0.leftPop(RK.FEED_BACK_LIST);


            FeedbackVo feedback = ConvertUtil.objToObj(obj,FeedbackVo.class);
            //消费逻辑
            log.info("消费消息:" + obj.toString());
            //处理完删除set集合
            ConFeedback conFeedback = new ConFeedback();
            conFeedback.setUuid(UUID.fromString(feedback.getId()));
            conFeedback.setSerMemo(feedback.getContent());
            conFeedback.setTitle(feedback.getTitle());
            conFeedback.setContactInfo(feedback.getContactInfo());
            conFeedback.setContent(feedback.getContent());
            conFeedback.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(conFeedback.getSerAudit())) {
                conFeedback.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(conFeedback.getSerUsing())) {
                conFeedback.setSerUsing(UsingFlag.ENABLE);
            }
            conFeedback.setUserID(feedback.getUserId());
            //  操作系统
            conFeedback.setSerOperatingSystem(feedback.getSerOperatingSystem());
            //  版本号
            conFeedback.setSerVersionNumber(feedback.getSerVersionNumber());
            Services.conFeedback.save(conFeedback);
            RedisClient.redisTemplate.opsForSet().pop(RK.FEED_BACK_LIST+"Set");
        }catch (Exception e){
            //主数据set 集合
            Set userScore= Jedis.db0.getSet(RK.SAVE_ORDER.concat("Set"));
            Iterator iterator = userScore.iterator();
            //消费逻辑
            log.info("消费消息:" + iterator.toString());
            //遍历set集合
            while(iterator.hasNext()){
                Map<String, Object> dataMap =(Map<String,Object>) iterator.next();
                Feedback feedback = ConvertUtil.objToObj(dataMap,Feedback.class);
                // 保存
                ConFeedback conFeedback = new ConFeedback();
                conFeedback.setUuid(UUID.fromString(feedback.getId()));
                conFeedback.setSerMemo(feedback.getContent());
                conFeedback.setTitle(feedback.getTitle());
                conFeedback.setContactInfo(feedback.getContactInfo());
                conFeedback.setContent(feedback.getContent());
                Services.conFeedback.save(conFeedback);

                Steward.initNullField(conFeedback);
                Services.conFeedback.save(conFeedback);
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.FEED_BACK_LIST+"Set");
        }

    }

}